# coding: utf-8
"""Qingstor
Usage:
  qingstor upload --path=<path>  --job_name=<job_name> --build_id=<build_id> [--namespace=<namespace> --product=<product>]
  qingstor upload_personal --path=<path> [--name=<name> --dirname=<dirname>]
  qingstor download_personal --path=<path> [--name=<name> --dirname=<dirname>]
  qingstor -h | --help
  qingstor --version

Options:
  -h --help     帮助.
  -v --version     查看版本号.
  --path=<path>  path设置 [default: Report/jacoco/jacoco-ut].
  --build_id=<build_id>  build_id设置.
  --job_name=<job_name>  job_name设置.
  --namespace=<namespace>  namespace设置 [default: coverage].
  --product=<product>  product设置 [default: stdci].
  --name=<name>  name设置 [default: qe].
  --dirname=<dirname>  dirname设置 [default: default].
"""
import os
from docopt import docopt
from qingstor.sdk.service.qingstor import QingStor
from qingstor.sdk.config import Config

#qy_access_key_id: 'RMLXUMGUETLZRFIWHJWO'
#qy_secret_access_key: 'YXMiI8qF9IYjUJvAEtYoyld7Xbh9uOGP00yoYdG3'


config = Config('RMLXUMGUETLZRFIWHJWO', 'YXMiI8qF9IYjUJvAEtYoyld7Xbh9uOGP00yoYdG3')
config.protocol = 'http'
config.host = '10.43.76.100'
config.host = 'stor.ecarx.com.cn'
config.port = 80
config.zone = 'ecarx'
qingstor = QingStor(config)

bucket = qingstor.Bucket("autobuildtestline", "ecarx")

def get_qingstor_path(path, job_name, build_id, namespace='coverage', product='stdci'):
    if path.startswith("/"):
        path = path[1:]
    qingstor_path = os.path.join(namespace, product, job_name, build_id, path)
    return qingstor_path

def upload(path, job_name, build_id, namespace='coverage', product='stdci'):
    for root, path, files in os.walk(path):
        for fi in files:
            file_path = os.path.join(root, fi)
            if file_path.startswith("/"):
                upload_path = file_path[1:]
            else:
                upload_path = file_path
            with open(file_path, 'rb') as f:
                output = bucket.put_object(
                    os.path.join(namespace, product, job_name, build_id, upload_path), body=f
                )
                print(file_path)


def upload_personal(path, name='qe', dirname='default'):
    # for root, path, files in os.walk(path):
    #     for fi in files:
    #         file_path = os.path.join(root, fi)
    #         if file_path.startswith("/"):
    #             file_path = file_path[1:]
    _path, filename = os.path.split(path)
    upload_to = os.path.join(name, dirname, filename)
    with open(path, 'rb') as f:
        output = bucket.put_object(
            upload_to, body=f
        )
        print(f"{path} --> {upload_to}")

def download_personal(path, name='qe', dirname='default'):
    # for root, path, files in os.walk(path):
    #     for fi in files:
    #         file_path = os.path.join(root, fi)
    #         if file_path.startswith("/"):
    #             file_path = file_path[1:]
    _path, filename = os.path.split(path)
    download_to = os.path.join(name, dirname, filename)
    with open(path, 'rb') as f:
        output = bucket.put_object(
            download_to, body=f
        )
        print(f"{path} --> {download_to}")

if __name__ == "__main__":
    arguments = docopt(__doc__, version="1.0.0")
    if arguments["upload"]:
        upload(arguments["--path"], job_name=arguments["--job_name"], build_id=arguments["--build_id"], namespace=arguments["--namespace"], product=arguments["--product"])
    if arguments["upload_personal"]:
        upload_personal(arguments["--path"], name=arguments["--name"], dirname=arguments["--dirname"])
    if arguments["download_personal"]:
        download_personal(arguments["--path"], name=arguments["--name"], dirname=arguments["--dirname"])